package org.testis.gui.mvc.view.testdesign;

import org.testis.gui.mvc.view.*;
import org.testis.gui.mvc.controller.testdesign.TreeController;
import org.testis.gui.mvc.controller.dynamicmap.DynamicMapTreeController;
import org.testis.gui.mvc.controller.*;
import org.testis.gui.mvc.utilities.DnDJTree;
import org.testis.gui.mvc.utilities.MyTreeRenderer;
import org.testis.gui.mvc.view.testbed.TbedItemPropertyPanel;
import org.testis.gui.mvc.view.testbed.TbedTreeViewPanel;
import org.testis.utils.tree.MyDefaultMutableTreeNode;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.beans.PropertyChangeEvent;
import java.util.LinkedList;
import javax.swing.JPanel;
import javax.swing.JOptionPane;
import javax.swing.JTree;
import javax.swing.KeyStroke;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;

import org.testis.orm.refinedclasses.DynamicMapDesignTreeNode;
import org.testis.orm.refinedclasses.TestDesignTreeNode;
import org.testis.utils.tree.TreeNode;

/**
 *
 * @author Cahit Emir Erel
 */
public class TreeViewPanel extends JPanel implements AbstractViewPanel {

    private static final long serialVersionUID = 4328321L;
    private MyDefaultMutableTreeNode m_jDesignTreeRoot;
    private MyDefaultMutableTreeNode m_jDynamicMapTreeRoot;
    private boolean m_compareModeOn = false;
    private boolean m_dMapCompareModeOn = false;
    private int mode = 0;
    private int dMapMode = 0;

    public TreeViewPanel() {
        super();
        m_jDesignTreeRoot = TestDesignControllerList.newInstance().
                getJDesignTreeRoot();
        m_jDynamicMapTreeRoot = DynamicMapControllerList.newInstance().
                getJDesignTreeRoot();
        initComponents();
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tabbedPane = new javax.swing.JTabbedPane();
        testDesignTab = new javax.swing.JPanel();
        jSplitPane1 = new javax.swing.JSplitPane();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        testDesignTree = new DnDJTree(m_jDesignTreeRoot,TestDesignControllerList.newInstance().getParentFrame(),TestDesignControllerList.newInstance().getTreeController());
        jPanel2 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        testDesignPropertyPane = new javax.swing.JTextPane();
        dynamicMapTab = new javax.swing.JPanel();
        jSplitPane2 = new javax.swing.JSplitPane();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        dynamicMapTree = new DnDJTree(m_jDynamicMapTreeRoot,DynamicMapControllerList.newInstance().getParentFrame(),DynamicMapControllerList.newInstance().getDynamicMapTreeController());
        jPanel4 = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        dynamicMapPropertyPane = new javax.swing.JTextPane();
        testBedTab = new javax.swing.JPanel();
        jSplitPane3 = new javax.swing.JSplitPane();
        jScrollPane5 = new javax.swing.JScrollPane();
        jScrollPane6 = new javax.swing.JScrollPane();

        tabbedPane.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "AirTies Test Automation Framework", javax.swing.border.TitledBorder.RIGHT, javax.swing.border.TitledBorder.BELOW_TOP, new java.awt.Font("Verdana", 1, 16))); // NOI18N
        tabbedPane.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                tabbedPaneStateChanged(evt);
            }
        });

        testDesignTab.setLayout(new javax.swing.BoxLayout(testDesignTab, javax.swing.BoxLayout.LINE_AXIS));

        jSplitPane1.setDividerLocation(300);

        testDesignTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
        testDesignTree.putClientProperty("JTree.lineStyle", "Angled");
        testDesignTree.setCellRenderer(new MyTreeRenderer());
        testDesignTree.setDragEnabled(true);
        testDesignTree.setExpandsSelectedPaths(false);
        testDesignTree.setRootVisible(false);
        testDesignTree.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                testDesignTreeMouseClicked(evt);
            }
        });
        testDesignTree.addTreeExpansionListener(new javax.swing.event.TreeExpansionListener() {
            public void treeCollapsed(javax.swing.event.TreeExpansionEvent evt) {
                testDesignTreeTreeCollapsed(evt);
            }
            public void treeExpanded(javax.swing.event.TreeExpansionEvent evt) {
                testDesignTreeTreeExpanded(evt);
            }
        });
        testDesignTree.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
            public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
                testDesignTreeValueChanged(evt);
            }
        });
        testDesignTree.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                testDesignTreeKeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(testDesignTree);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 299, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 634, Short.MAX_VALUE)
        );

        jSplitPane1.setLeftComponent(jPanel1);

        testDesignPropertyPane.setEditable(false);
        jScrollPane2.setViewportView(testDesignPropertyPane);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 403, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 634, Short.MAX_VALUE)
        );

        jSplitPane1.setRightComponent(jPanel2);

        testDesignTab.add(jSplitPane1);

        tabbedPane.addTab("Test Designer", testDesignTab);

        dynamicMapTab.setLayout(new javax.swing.BoxLayout(dynamicMapTab, javax.swing.BoxLayout.LINE_AXIS));

        jSplitPane2.setDividerLocation(300);

        dynamicMapTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
        dynamicMapTree.putClientProperty("JTree.lineStyle", "Angled");
        dynamicMapTree.setCellRenderer(new MyTreeRenderer());
        dynamicMapTree.setDragEnabled(true);
        dynamicMapTree.setExpandsSelectedPaths(false);
        dynamicMapTree.setRootVisible(false);
        dynamicMapTree.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                dynamicMapTreeMouseClicked(evt);
            }
        });
        dynamicMapTree.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
            public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
                dynamicMapTreeValueChanged(evt);
            }
        });
        dynamicMapTree.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                dynamicMapTreeKeyPressed(evt);
            }
        });
        jScrollPane3.setViewportView(dynamicMapTree);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 299, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 634, Short.MAX_VALUE)
        );

        jSplitPane2.setLeftComponent(jPanel3);

        dynamicMapPropertyPane.setEditable(false);
        jScrollPane4.setViewportView(dynamicMapPropertyPane);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 403, Short.MAX_VALUE)
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 634, Short.MAX_VALUE)
        );

        jSplitPane2.setRightComponent(jPanel4);

        dynamicMapTab.add(jSplitPane2);

        tabbedPane.addTab("Dynamic Map Designer", dynamicMapTab);

        testBedTab.setLayout(new javax.swing.BoxLayout(testBedTab, javax.swing.BoxLayout.LINE_AXIS));

        jSplitPane3.setDividerLocation(300);

        jScrollPane5.setViewportView(new TbedTreeViewPanel());
        jSplitPane3.setLeftComponent(jScrollPane5);

        jScrollPane6.setViewportView(new TbedItemPropertyPanel());
        jSplitPane3.setRightComponent(jScrollPane6);

        testBedTab.add(jSplitPane3);

        tabbedPane.addTab("Test Bed Configuration", testBedTab);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(tabbedPane, javax.swing.GroupLayout.Alignment.TRAILING)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(tabbedPane)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void testDesignTreeMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_testDesignTreeMouseClicked
        if (evt.getButton() == MouseEvent.BUTTON3) {
            TreePath path = testDesignTree.getPathForLocation(evt.getX(),
                    evt.getY());
            testDesignTree.setSelectionPath(path);
            MyDefaultMutableTreeNode node =
                    (MyDefaultMutableTreeNode) testDesignTree.getLastSelectedPathComponent();
            if (node != null) {
                LinkedList coordinates = new LinkedList();
                coordinates.add(evt.getComponent());
                coordinates.add(evt.getX());
                coordinates.add(evt.getY());
                TestDesignControllerList.newInstance().getPopupController().
                        operationOpenPopupMenu(node,
                        coordinates);
            }
            TestDesignControllerList.newInstance().getTreeController().
                    operationSelectDesignPart(node,
                    mode);

        } else if (evt.getButton() == MouseEvent.BUTTON1) {
            MyDefaultMutableTreeNode node =
                    (MyDefaultMutableTreeNode) testDesignTree.getLastSelectedPathComponent();
            int x = evt.getX();
            int y = evt.getY();
            int row = testDesignTree.getRowForLocation(x,
                    y);
            TreePath path = testDesignTree.getPathForRow(row);
            if (path != null && node != null) {
                if (evt.getX() > testDesignTree.getPathBounds(path).x + 21) {
                    return;
                }
                boolean isSelected = !(node.isSelected());
                node.setSelected(isSelected);
                node.setSelectedSingle(isSelected);
                if (isSelected) {
                    testDesignTree.expandPath(path);
                } else {
                    testDesignTree.collapsePath(path);
                }
                testDesignTree.revalidate();
                testDesignTree.repaint();

//                TestDesignControllerList.newInstance().getTreeController().
//                    operationRefreshTree();
            }

        }
}//GEN-LAST:event_testDesignTreeMouseClicked

    private void testDesignTreeValueChanged(javax.swing.event.TreeSelectionEvent evt) {//GEN-FIRST:event_testDesignTreeValueChanged
        MyDefaultMutableTreeNode node =
                (MyDefaultMutableTreeNode) testDesignTree.getLastSelectedPathComponent();
        TestDesignControllerList.newInstance().getTreeController().
                operationSelectDesignPart(node,
                mode);
}//GEN-LAST:event_testDesignTreeValueChanged

    private void dynamicMapTreeMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_dynamicMapTreeMouseClicked
        TreePath dynamicMapTreeSelPath =
                dynamicMapTree.getPathForLocation(evt.getX(),
                evt.getY());
        if (evt.getButton() == MouseEvent.BUTTON3) {
            dynamicMapTree.setSelectionPath(dynamicMapTreeSelPath);
            MyDefaultMutableTreeNode node =
                    (MyDefaultMutableTreeNode) dynamicMapTree.getLastSelectedPathComponent();
            if (node != null) {
                DynamicMapControllerList.newInstance().getDynamicMapTreeController().
                        operationSelectDesignPart(node,
                        dMapMode);
                //System.out.println("DYNAMIC MAP POPUP MENU");
                LinkedList coordinates = new LinkedList();
                coordinates.add(evt.getComponent());
                coordinates.add(evt.getX());
                coordinates.add(evt.getY());
                DynamicMapControllerList.newInstance().
                        getDynamicMapPopupController().operationOpenPopupMenu(
                        node,
                        coordinates);
            }
        } else if (evt.getButton() == MouseEvent.BUTTON1) {
            int x = evt.getX();
            int y = evt.getY();
            int row = dynamicMapTree.getRowForLocation(x,
                    y);
            TreePath path = dynamicMapTree.getPathForRow(row);
            MyDefaultMutableTreeNode node = null;
            if (path != null) {
                node = (MyDefaultMutableTreeNode) path.getLastPathComponent();
            }
            //TreePath  path = tree.getSelectionPath();
            if (path != null && node != null) {

                boolean isSelected = !(node.isSelected());
                node.setSelected(isSelected);
                if (isSelected) {
                    dynamicMapTree.expandPath(path);
                } else {
                    dynamicMapTree.collapsePath(path);
                }
                ((DefaultTreeModel) dynamicMapTree.getModel()).nodeChanged(node);
                // I need revalidate if node is root.  but why?
                if (row == 0) {
                    dynamicMapTree.revalidate();
                    dynamicMapTree.repaint();
//                    DynamicMapControllerList.newInstance().getDynamicMapTreeController().
//                    operationRefreshTree();
                }
            }
        }
}//GEN-LAST:event_dynamicMapTreeMouseClicked

    private void dynamicMapTreeValueChanged(javax.swing.event.TreeSelectionEvent evt) {//GEN-FIRST:event_dynamicMapTreeValueChanged
        MyDefaultMutableTreeNode node =
                (MyDefaultMutableTreeNode) dynamicMapTree.getLastSelectedPathComponent();
        DynamicMapControllerList.newInstance().getDynamicMapTreeController().
                operationSelectDesignPart(node,
                dMapMode);
}//GEN-LAST:event_dynamicMapTreeValueChanged

private void tabbedPaneStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_tabbedPaneStateChanged
}//GEN-LAST:event_tabbedPaneStateChanged

private void testDesignTreeTreeCollapsed(javax.swing.event.TreeExpansionEvent evt) {//GEN-FIRST:event_testDesignTreeTreeCollapsed

    changeTreePath();
}//GEN-LAST:event_testDesignTreeTreeCollapsed

private void testDesignTreeTreeExpanded(javax.swing.event.TreeExpansionEvent evt) {//GEN-FIRST:event_testDesignTreeTreeExpanded

    changeTreePath();
}//GEN-LAST:event_testDesignTreeTreeExpanded

private void testDesignTreeKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_testDesignTreeKeyPressed
    if (evt.getKeyCode() == KeyEvent.VK_F3) {
        TestDesignControllerList.newInstance().getTreeController().operationExpandToNextNode();
    } else if (evt.getKeyCode() == KeyStroke.getKeyStroke(KeyEvent.VK_F,
            KeyEvent.CTRL_MASK).getKeyCode()) {
        String result = javax.swing.JOptionPane.showInputDialog("Enter Value:");
        if (result == null) {
            return;
        }
        if (!result.equals("")) {
            TestDesignControllerList.newInstance().getTreeController().operationChangeLastSearch(
                    result.trim());
            TestDesignControllerList.newInstance().getTreeController().operationExpandToNextNode();
        }
    } else if (evt.getKeyCode() == KeyStroke.getKeyStroke(KeyEvent.VK_B,
            KeyEvent.CTRL_MASK).getKeyCode()) {
        TestDesignControllerList.newInstance().getTreeController().operationChangeLastSearch(
                "");
        TestDesignControllerList.newInstance().getTreeController().operationValidateTree();
    }
}//GEN-LAST:event_testDesignTreeKeyPressed

private void dynamicMapTreeKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_dynamicMapTreeKeyPressed
    if (evt.getKeyCode() == KeyEvent.VK_F3) {
        DynamicMapControllerList.newInstance().getDynamicMapTreeController().operationExpandToNextNode();
    } else if (evt.getKeyCode() == KeyStroke.getKeyStroke(KeyEvent.VK_F,
            KeyEvent.CTRL_MASK).getKeyCode()) {
        String result = javax.swing.JOptionPane.showInputDialog("Enter Value:");
        if (result == null) {
            return;
        }
        if (!result.equals("")) {
            DynamicMapControllerList.newInstance().getDynamicMapTreeController().operationChangeLastSearch(
                    result.trim());
            DynamicMapControllerList.newInstance().getDynamicMapTreeController().operationExpandToNextNode();
        }
    } else if (evt.getKeyCode() == KeyStroke.getKeyStroke(KeyEvent.VK_B,
            KeyEvent.CTRL_MASK).getKeyCode()) {
        DynamicMapControllerList.newInstance().getDynamicMapTreeController().operationChangeLastSearch(
                "");
        DynamicMapControllerList.newInstance().getDynamicMapTreeController().operationValidateTree();
    }
}//GEN-LAST:event_dynamicMapTreeKeyPressed

    private void changeTreePath() {
        TreePath treePath = testDesignTree.getSelectionPath();
        if (treePath != null) {
            TestDesignControllerList.newInstance().getTreeController().
                    operationTreePathChanged(treePath);
        }
    }

    @Override
    public void modelPropertyChange(PropertyChangeEvent evt) {
//        System.out.println(evt.getPropertyName());
        if (evt.getPropertyName().equals(TreeController.TREE_LOAD)) {
            MyDefaultMutableTreeNode node =
                    (MyDefaultMutableTreeNode) evt.getNewValue();
            if (node.getUserObject() instanceof TestDesignTreeNode) {
                loadJTestDesignTree(node, testDesignTree);
            }
        } else if (evt.getPropertyName().equals(
                DynamicMapTreeController.TREE_LOAD)) {
            MyDefaultMutableTreeNode node =
                    (MyDefaultMutableTreeNode) evt.getNewValue();
            if (node.getUserObject() instanceof DynamicMapDesignTreeNode) {
                loadJDynamicMapTree(node, testDesignTree);
            }
        } else if (evt.getPropertyName().equals(TreeController.TREE_SAVE)) {
            MyDefaultMutableTreeNode node = TestDesignControllerList.newInstance().
                    getTreeController().getSelectedMutableTreeNode();
//            MyDefaultMutableTreeNode node =
//                (MyDefaultMutableTreeNode) testDesignTree.
//                getLastSelectedPathComponent();
            MyDefaultMutableTreeNode child =
                    (MyDefaultMutableTreeNode) evt.getNewValue();
            Integer place =
                    ((TestDesignTreeNode) child.getUserObject()).getPlace();
            if (place != null) {
                node.insert(child,
                        place);
            } else {
                node.add(child);
            }
            child.setSelected(node.isSelected());
            TestDesignControllerList.newInstance().getTreeController().
                    operationSelectDesignPart(child,
                    mode);
            //testDesignTree.invalidate();
        } else if (evt.getPropertyName().equals(
                DynamicMapTreeController.TREE_SAVE)) {
            MyDefaultMutableTreeNode node =
                    DynamicMapControllerList.newInstance().
                    getDynamicMapTreeController().operationGetSelectedNode();
//            MyDefaultMutableTreeNode node =
//                (MyDefaultMutableTreeNode) dynamicMapTree.
//                getLastSelectedPathComponent();
            MyDefaultMutableTreeNode child =
                    (MyDefaultMutableTreeNode) evt.getNewValue();
            Integer place =
                    ((DynamicMapDesignTreeNode) child.getUserObject()).getPlace();
            if (place != null) {
                node.insert(child,
                        place);
            } else {
                node.add(child);
            }
            child.setSelected(node.isSelected());
            DynamicMapControllerList.newInstance().getDynamicMapTreeController().
                    operationSelectDesignPart(child,
                    dMapMode);
            //dynamicMapTree.invalidate();
        } else if (evt.getPropertyName().equals(
                DynamicMapTreeController.TREE_REFRESH)) {
            if (DynamicMapControllerList.newInstance().getDynamicMapTreeController().operationGetSelectedDesignPart() != null) {
                dynamicMapPropertyPane.setText(DynamicMapControllerList.newInstance().getDynamicMapTreeController().operationGetSelectedDesignPart().toString());
                dynamicMapPropertyPane.invalidate();
            }
            dynamicMapTree.updateUI();
        } else if (evt.getPropertyName().equals(TreeController.TREE_REFRESH)) {
            if (TestDesignControllerList.newInstance().getTreeController().getSelectedTreeNode() != null) {
                testDesignPropertyPane.setText(TestDesignControllerList.newInstance().getTreeController().getSelectedTreeNode().toString());
                testDesignPropertyPane.invalidate();
            }
            testDesignTree.updateUI();
        } else if (evt.getPropertyName().equals(
                DynamicMapTreeController.TREE_RELOAD)) {
            dynamicMapTree.updateUI();
        } else if (evt.getPropertyName().equals(TreeController.TREE_RELOAD)) {

            testDesignTree.updateUI();
        } else if (evt.getPropertyName().equals(
                TreeController.RELOAD_TREE_PATH)) {
            TreePath path =
                    (TreePath) evt.getNewValue();
            testDesignTree.setSelectionPath(path);
            testDesignTree.collapsePath(path);
            testDesignTree.expandPath(path);

        } else if (evt.getPropertyName().equals(
                TreeController.SELECT_DESIGN_PART)) {
            MyDefaultMutableTreeNode node =
                    (MyDefaultMutableTreeNode) evt.getNewValue();
            if (node != null) {
                TreePath treePath = new TreePath(node.getPath());
                testDesignTree.setSelectionPath(treePath);
                testDesignTree.collapsePath(treePath);
                testDesignTree.expandPath(treePath);
            }
        } else if (evt.getPropertyName().equals(
                DynamicMapTreeController.SELECT_DESIGN_PART)) {
            MyDefaultMutableTreeNode node =
                    (MyDefaultMutableTreeNode) evt.getNewValue();
            if (node != null) {
                TreePath treePath = new TreePath(node.getPath());
                dynamicMapTree.setSelectionPath(treePath);
                dynamicMapTree.collapsePath(treePath);
                dynamicMapTree.expandPath(treePath);
                /* System.out.println("currentPath:" + dynamicMapTree.getSelectionPath().
                toString());*/
            } else {
                System.out.println("node is null");
            }
        } else if (evt.getPropertyName().equals(
                TreeController.SHOW_PROPERTIES)) {
            testDesignPropertyPane.setText(evt.getNewValue().toString());
            testDesignPropertyPane.invalidate();
        } else if (evt.getPropertyName().equals(
                TreeController.COMPARE_MODE_ON)) {
            Boolean val = (Boolean) evt.getNewValue();
            if (val != null) {
                setCompareModeOn(val.booleanValue());
            }
        } else if (evt.getPropertyName().equals(
                DynamicMapTreeController.COMPARE_MODE_ON)) {
            Boolean val = (Boolean) evt.getNewValue();
            if (val != null) {
                setDMapCompareModeOn(val.booleanValue());
            }
        } else if (evt.getPropertyName().equals(
                DynamicMapTreeController.SHOW_PROPERTIES)) {
            dynamicMapPropertyPane.setText(evt.getNewValue().toString());
            dynamicMapPropertyPane.invalidate();
        } else if (evt.getPropertyName().equals(TreeController.NODE_DELETE)) {
            testDesignTree.clearSelection();
        } else if (evt.getPropertyName().equals(
                DynamicMapTreeController.NODE_DELETE)) {
            dynamicMapTree.clearSelection();
        } else if (evt.getPropertyName().equals(TreeController.SHOW_TAB)) {
            Integer tab = (Integer) evt.getNewValue();
            tabbedPane.setEnabledAt(tab,
                    true);
            tabbedPane.setSelectedIndex(tab.intValue());
        } else if (evt.getPropertyName().equals(TreeController.HIDE_TAB)) {
            Integer tab = (Integer) evt.getNewValue();
            tabbedPane.setEnabledAt(tab,
                    false);
        } else if (evt.getPropertyName().equals(
                TreeController.CLEAR_SELECTION_PROPERTY)) {
            Boolean value = (Boolean) evt.getNewValue();
            if (value != null && value) {
                testDesignTree.clearSelection();
            }
        } else if (evt.getPropertyName().equals(
                DynamicMapTreeController.CLEAR_SELECTION_PROPERTY)) {
            Boolean value = (Boolean) evt.getNewValue();
            if (value != null && value) {
                dynamicMapTree.clearSelection();
            }
        }
    }    // </editor-fold>
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextPane dynamicMapPropertyPane;
    private javax.swing.JPanel dynamicMapTab;
    private javax.swing.JTree dynamicMapTree;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JScrollPane jScrollPane6;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JSplitPane jSplitPane2;
    private javax.swing.JSplitPane jSplitPane3;
    private javax.swing.JTabbedPane tabbedPane;
    private javax.swing.JPanel testBedTab;
    private javax.swing.JTextPane testDesignPropertyPane;
    private javax.swing.JPanel testDesignTab;
    private javax.swing.JTree testDesignTree;
    // End of variables declaration//GEN-END:variables

    private void loadJTestDesignTree(MyDefaultMutableTreeNode root,
            JTree jTree1) {
        MyDefaultMutableTreeNode tempNode = new MyDefaultMutableTreeNode(true);
        convert(root, tempNode, true);
        getJDesignTreeRoot().add(
                (MyDefaultMutableTreeNode) tempNode.getFirstChild());
        jTree1.updateUI();
    }

    public static void showMessageDialog(String message,
            String title) {
        JOptionPane.showMessageDialog(null,
                message,
                title,
                JOptionPane.ERROR_MESSAGE);
    }

    public static boolean showConfirmDialogBox(String message,
            String title) {
        int result =
                JOptionPane.showConfirmDialog(null,
                message,
                title,
                javax.swing.JOptionPane.YES_NO_OPTION,
                javax.swing.JOptionPane.QUESTION_MESSAGE);
        if (result == JOptionPane.YES_OPTION) {
            return true;
        } else {
            return false;
        }
    }

    private void loadJDynamicMapTree(MyDefaultMutableTreeNode root,
            JTree jTree1) {
        MyDefaultMutableTreeNode tempNode = new MyDefaultMutableTreeNode(false);
        convert(root,
                tempNode,
                false);
        m_jDynamicMapTreeRoot.add(
                (MyDefaultMutableTreeNode) tempNode.getFirstChild());
        jTree1.updateUI();
    }

    private void convert(MyDefaultMutableTreeNode node,
            MyDefaultMutableTreeNode parent,
            boolean hasCheckBox) {
        TreeNode treeNode = (TreeNode) node.getUserObject();
        parent.add(node);
        if (treeNode.hasChildren()) {
            TreeNode[] array = treeNode.getChildren();
            for (int i = 0; i < array.length; i++) {
                convert(new MyDefaultMutableTreeNode(array[i],
                        hasCheckBox),
                        node,
                        hasCheckBox);
            }
        }
    }

    public MyDefaultMutableTreeNode getJDesignTreeRoot() {
        return m_jDesignTreeRoot;
    }

    public void setJDesignTreeRoot(MyDefaultMutableTreeNode jDesignTreeRoot) {
        m_jDesignTreeRoot = jDesignTreeRoot;
    }

    public boolean isCompareModeOn() {
        return m_compareModeOn;
    }

    private void setCompareModeOn(boolean compareModeOn) {
        m_compareModeOn = compareModeOn;
        if (compareModeOn) {
            mode = 1;
        } else {
            mode = 0;
        }
    }

    public boolean isDMapCompareModeOn() {
        return m_dMapCompareModeOn;
    }

    private void setDMapCompareModeOn(boolean dMapCompareModeOn) {
        m_dMapCompareModeOn = dMapCompareModeOn;
        if (dMapCompareModeOn) {
            dMapMode = 1;
        } else {
            dMapMode = 0;
        }
    }
}
